/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.SubjectOfferingDAO;
import com.tces.controller.SubjectScheduleDAO;
import com.tces.model.SubjectOffering;
import com.tces.model.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author Arnold
 */
public class SubjectOfferingAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(SubjectOfferingAction.class);
    private List<SubjectOffering> subjectOfferingList;
    private User user;
    private List<SubjectOffering> subjectOfferingNameList = new ArrayList<SubjectOffering> ();
    private String id, oper, collegeCode, subjectOfferingName, subjectOfferingSyFrom, subjectOfferingSyTo, subjectOfferingSemester,subjectOfferingStatus;
@Override
    public String execute() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            log.info("User role:" + user.getRoleName());
            if (user.getRoleName().equalsIgnoreCase("Administrator") || user.getRoleName().equalsIgnoreCase("Guidance Counseling Head")) {
                subjectOfferingNameList = SubjectOfferingDAO.getFullname();
            } else {
                subjectOfferingNameList = SubjectOfferingDAO.getFullname(user.getCollegeID());
            }
            
        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }
    public String view() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            subjectOfferingList = SubjectOfferingDAO.view();

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");


            if (oper.equalsIgnoreCase("add")) {

                if (SubjectOfferingDAO.add(getSubjectOffering()) > 0) {
                    addActionMessage("Successfull insert of Subject Offering(" + getSubjectOffering().getSubjectOfferingName() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {

                if (SubjectOfferingDAO.update(getSubjectOffering()) > 0) {
                    addActionMessage("Successfull update of Subject Offering(" + getSubjectOffering().getSubjectOfferingName() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
                if (SubjectOfferingDAO.delete(Integer.parseInt(id)) > 0) {
                    addActionMessage("Successfull deletion of Subject Offering[ID:" + id + "]");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    private SubjectOffering getSubjectOffering() {
        SubjectOffering subjectOffering = new SubjectOffering();
        if (collegeCode != null) {
            subjectOffering.setCollegeID(Integer.parseInt(collegeCode));
        }
        if (subjectOfferingSyFrom != null) {
            subjectOffering.setSubjectOfferingSyFrom(Integer.parseInt(subjectOfferingSyFrom));
        }
        if (subjectOfferingSyTo != null) {
            subjectOffering.setSubjectOfferingSyTo(Integer.parseInt(subjectOfferingSyTo));
        }
        if (subjectOfferingSemester != null) {
            subjectOffering.setSubjectOfferingSemester(Integer.parseInt(subjectOfferingSemester));
        }
        if (id != null && !(id.equalsIgnoreCase("_empty"))) {
            log.info(""+id.equalsIgnoreCase("_empty"));
            subjectOffering.setSubjectOfferingID(Integer.parseInt(id));
        }
        subjectOffering.setSubjectOfferingName(subjectOfferingName);
        subjectOffering.setSubjectOfferingStatus(Integer.parseInt(subjectOfferingStatus));

        return subjectOffering;
    }

    public List<SubjectOffering> getSubjectOfferingList() {
        return subjectOfferingList;
    }

    public void setSubjectOfferingList(List<SubjectOffering> subjectOfferingList) {
        this.subjectOfferingList = subjectOfferingList;
    }

    public String getCollegeCode() {
        return collegeCode;
    }

    public void setCollegeCode(String collegeCode) {
        this.collegeCode = collegeCode;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public String getSubjectOfferingName() {
        return subjectOfferingName;
    }

    public void setSubjectOfferingName(String subjectOfferingName) {
        this.subjectOfferingName = subjectOfferingName;
    }

    public String getSubjectOfferingSemester() {
        return subjectOfferingSemester;
    }

    public void setSubjectOfferingSemester(String subjectOfferingSemester) {
        this.subjectOfferingSemester = subjectOfferingSemester;
    }

    public String getSubjectOfferingSyFrom() {
        return subjectOfferingSyFrom;
    }

    public void setSubjectOfferingSyFrom(String subjectOfferingSyFrom) {
        this.subjectOfferingSyFrom = subjectOfferingSyFrom;
    }

    public String getSubjectOfferingSyTo() {
        return subjectOfferingSyTo;
    }

    public void setSubjectOfferingSyTo(String subjectOfferingSyTo) {
        this.subjectOfferingSyTo = subjectOfferingSyTo;
    }

    public String getSubjectOfferingStatus() {
        return subjectOfferingStatus;
    }

    public void setSubjectOfferingStatus(String subjectOfferingStatus) {
        this.subjectOfferingStatus = subjectOfferingStatus;
    }

    public List<SubjectOffering> getSubjectOfferingNameList() {
        return subjectOfferingNameList;
    }

    public void setSubjectOfferingNameList(List<SubjectOffering> subjectOfferingNameList) {
        this.subjectOfferingNameList = subjectOfferingNameList;
    }

    
}
